package com.ruanjian2001.Dao;

import com.ruanjian2001.entity.Order;
import com.ruanjian2001.entity.User;
import com.ruanjian2001.util.JDBCUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class OrderDao {
    /**
     * 根据用户查找用户订单
     * @param user 要查找订单的用户
     * @return 订单集合
     */
    public List<Order> findOrderByUser(User user){
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        List<Order> list = new ArrayList();
       try {
           conn = JDBCUtil.getConnection();
           String sql = "select * from orders where user_id = ?";
           stmt = conn.prepareStatement(sql);
           stmt.setInt(1,user.getId());
           
           rs = stmt.executeQuery();

           while(rs.next()){
               Order order = new Order();
               order.setId(rs.getString("id"));
               order.setMoney(rs.getDouble("money"));
               order.setReceiverAddress(rs.getString("receiverAddress"));
               order.setReceiverName(rs.getString("receiverName"));
               order.setPaystate(rs.getInt("paystate"));
               order.setOrdertime(rs.getDate("ordertime"));
               order.setUser(user);
               System.out.println(order);
               list.add(order);
           }

       }catch(Exception e){
           e.printStackTrace();
       }finally{
           JDBCUtil.close(conn,stmt,rs);
        }
        return list;
    }


 }
